package cn.codeinn.service1.service.impl;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.codeinn.generator.service.impl.ServiceImpl;
import cn.codeinn.generator.util.Page;
import cn.codeinn.model.vo.MemberVO;
import cn.codeinn.service1.entity.Member;
import cn.codeinn.service1.mapper.MemberMapper;
import cn.codeinn.service1.service.AsyncService;
import cn.codeinn.service1.service.MemberService;

@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

	@Autowired
	private AsyncService asyncService;

	@Override
	public Page<Member> queryPage(Page<Member> page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		List<Member> list = mapper.select(page.getCondition(Member.class));
		page.setPage(new PageInfo<>(list));
		return page;
	}

	//@Cacheable(value = "memberCache", key = "'SELECTMEMBER-'+#id")
	@Override
	public MemberVO selectMember(Integer id) {
//		try {
//			Thread.sleep(10000);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
		System.out.println("查询数据库获取用户信息：");
		Member member = mapper.selectByPrimaryKey(id);
		MemberVO vo = new MemberVO();
		if (member != null) {
			vo.setId(member.getId());
			vo.setName(member.getName());
			vo.setUsername(member.getUsername());
		}
		return vo;
	}

	@Override
	public void doTestAsync() {
		System.out.println("start do something...");
		Future<String> future = asyncService.testAsync1();
		asyncService.testAsync2();
		System.out.println("end the main method ...");
		while (true) {
			if (!future.isDone()) {
				try {
					System.out.println("test1 result ---->" + future.get());
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
				break;
			}
		}
	}

}
